﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
using Restarty_SIPO3.Class;

namespace Restarty_SIPO3.Forms
{
    public partial class Nacitavanie : Form
    {
        private string CestaKSuborom;

        public Nacitavanie()
        {
            InitializeComponent();
            if (!bwSpracovanieSuborov.IsBusy)
                bwSpracovanieSuborov.RunWorkerAsync();
        }

        public Nacitavanie(string CestaKSuborom)
        {
            this.CestaKSuborom = CestaKSuborom;
            InitializeComponent();
            if (!bwSpracovanieSuborov.IsBusy)
                bwSpracovanieSuborov.RunWorkerAsync();
        }

        private void bwSpracovanieSuborov_DoWork(object sender, DoWorkEventArgs e)
        {
            nastavenia nastav = new nastavenia();
            ArrayList Subory = new ArrayList();
            PocetSuborovCelkom = 0;
            foreach (String stredisko in nastav.Strediska)
            {
                PocetSuborovStrediska = 0;
                Subory = new ArrayList();
                DirectoryInfo Adresar = new DirectoryInfo(CestaKSuborom + "\\" + stredisko + "\\txt\\");
                Subory.AddRange(Adresar.GetFiles("*.txt"));
                PocetSuborovCelkom += Subory.Count;
                //PocetSuborovStrediska += Subory.Count;
                Subory.Clear();
            }
            bwSpracovanieSuborov.ReportProgress(0);

            foreach (String stredisko in nastav.Strediska)
            {
                DirectoryInfo Adresar = new DirectoryInfo(CestaKSuborom + "\\" + stredisko + "\\txt\\");
                Subory.AddRange(Adresar.GetFiles("*.txt"));
            }
            foreach (FileInfo subor in Subory)
            {
                if (!bwSpracovanieSuborov.CancellationPending)
                {
                    SpracovanieSuboru sub = new SpracovanieSuboru(subor);
                    if (sub.AnalyzaSuboru(zistiStredisko(subor.FullName)))
                        MessageBox.Show(sub.chyba.ErrorText, "Chyba", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    if (sub.PridaneDoDB())
                        MessageBox.Show(sub.chyba.ErrorText, "Chyba", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    bwSpracovanieSuborov.ReportProgress(1);
                }
            }
        }

        private string zistiStredisko(string p)
        {
            String tmpStr = p.Remove(p.IndexOf("\\txt\\"));
            tmpStr = tmpStr.Substring(tmpStr.LastIndexOf("\\")+1);
            return tmpStr;
        }

        public int PocetSuborovCelkom { get; set; }

        public int PocetSuborovStrediska { get; set; }

        private void bwSpracovanieSuborov_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            switch (e.ProgressPercentage)
            {
                case 0: nastavHodnoty();
                    break;
                case 1: inkrementujNacitaneSubory();
                    break;
            }
        }

        private void inkrementujNacitaneSubory()
        {
            int tmpInt=int.Parse(lbPocetSuborovN.Text);
            tmpInt++;
            lbPocetSuborovN.Text = tmpInt.ToString();
            pbSubory.Value = tmpInt;
        }

        private void nastavHodnoty()
        {
            lbPocetSrediskoC.Text = PocetSuborovStrediska.ToString();
            lbPocetSuborovC.Text = PocetSuborovCelkom.ToString();
            pbSubory.Maximum = PocetSuborovCelkom;
            pbStrediska.Maximum = PocetSuborovStrediska;
        }

        private void tmAnimacia_Tick(object sender, EventArgs e)
        {
            Image oldImage = pbProgress.Image;
            pbProgress.Image = Class.PracaSObrazkom.RotateImage(oldImage, 15);
        }

        private void btnZrus_Click(object sender, EventArgs e)
        {
            bwSpracovanieSuborov.CancelAsync();
        }

        private void bwSpracovanieSuborov_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.Close();
        }
    }
}
